//
//  HTClassQuitSmokingVC.swift
//  QuitSmokingApp
//
//  Created by   on 2024/3/11.
//

import UIKit

class HTClassQuitSmokingVC: HTClassViewController {
    lazy var var_bgView: UIView = {
        let var_view = UIView()
        var_view.backgroundColor = .black
        return var_view
    }()
    
    /// 每日卷烟消耗量
    lazy var var_DailyCigaretteTF: UITextField = {
        let var_textF = UITextField()
        var_textF.textColor = .white
        var_textF.placeholder = "please enter"
        var_textF.keyboardType = .numberPad
        var_textF.textAlignment = .right
        var_textF.font = .systemFont(ofSize: 14, weight: .medium)
        var_textF.attributedPlaceholder = NSAttributedString.init(string:"please enter", attributes: [
                    NSAttributedString.Key.foregroundColor:UIColor("#797673")])
        return var_textF
    }()
    
    /// 香烟品牌
    lazy var var_CigaretteBrandTF: UITextField = {
        let var_textF = UITextField()
        var_textF.textColor = .white
        var_textF.placeholder = "please enter"
        var_textF.textAlignment = .right
        var_textF.font = .systemFont(ofSize: 14, weight: .medium)
        var_textF.attributedPlaceholder = NSAttributedString.init(string:"please enter", attributes: [
                    NSAttributedString.Key.foregroundColor:UIColor("#797673")])
        return var_textF
    }()

    /// 香烟单价
    lazy var var_CigarettePriceLabel: UITextField = {
        let var_textF = UITextField()
        var_textF.textColor = .white
        var_textF.placeholder = "Please click on Settings"
        var_textF.textAlignment = .right
        var_textF.font = .systemFont(ofSize: 14, weight: .medium)
        var_textF.attributedPlaceholder = NSAttributedString.init(string:"Please click on Settings", attributes: [
                    NSAttributedString.Key.foregroundColor:UIColor("#797673")])
        
        return var_textF
    }()
    
    /// 香烟焦油含量
    lazy var var_mgLabel: UITextField = {
        let var_textF = UITextField()
        var_textF.textColor = .white
        var_textF.placeholder = "please enter"
        var_textF.keyboardType = .numberPad
        var_textF.textAlignment = .right
        var_textF.font = .systemFont(ofSize: 14, weight: .medium)
        var_textF.attributedPlaceholder = NSAttributedString.init(string:"please enter", attributes: [
                    NSAttributedString.Key.foregroundColor:UIColor("#797673")])
        return var_textF
    }()
    /// 价格
    var var_price: String = ""
    /// 每包香烟数量
    var var_total_number: String = ""
    
    var var_dataModel: SmokingCessationPlanModel?
    override func viewDidLoad() {
        super.viewDidLoad()
        title = "Quit smoking"
    }
    override func ht_setUI() {
        super.ht_setUI()
        view.addSubview(var_bgView)
        var_bgView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalToSuperview().offset(HTClassTool.STATIC_navigationBarTotalHeight)
        }
        
        ht_DailyCigaretteConsumption()
        ht_CigaretteBrand()
        ht_CigarettePriceLabel()
        ht_mgLabel()
        
        let var_saveBtn = UIButton()
        var_saveBtn.setTitle("Save", for: .normal)
        var_saveBtn.setTitleColor(.white, for: .normal)
        var_saveBtn.titleLabel?.font = .systemFont(ofSize: 20, weight: .bold)
        var_saveBtn.layer.cornerRadius = 10
        var_saveBtn.layer.masksToBounds = true
        var_saveBtn.onTap { [weak self] in
            guard let var_dailyCigaretteConsumption = self?.var_DailyCigaretteTF.text else {
                HTClassHudView().ht_showToastOnView(var_view: self!.view, var_title: "Please enter the daily cigarette consumption", var_completedBlock: nil).hide(animated: true, afterDelay: var_kHudHiddenTime)
                return
            }
            if var_dailyCigaretteConsumption.count == 0 {
                HTClassHudView().ht_showToastOnView(var_view: self!.view, var_title: "Please enter the daily cigarette consumption", var_completedBlock: nil).hide(animated: true, afterDelay: var_kHudHiddenTime)
                return
            }
            guard let var_cigaretteBrand = self?.var_CigaretteBrandTF.text else {
                HTClassHudView().ht_showToastOnView(var_view: self!.view, var_title: "Please enter the cigarette brand", var_completedBlock: nil).hide(animated: true, afterDelay: var_kHudHiddenTime)
                return
            }
            if var_cigaretteBrand.count == 0 {
                HTClassHudView().ht_showToastOnView(var_view: self!.view, var_title: "Please enter the cigarette brand", var_completedBlock: nil).hide(animated: true, afterDelay: var_kHudHiddenTime)
                return
            }
            guard let var_cigarettePrice = self?.var_CigarettePriceLabel.text else {
                HTClassHudView().ht_showToastOnView(var_view: self!.view, var_title: "Please set cigarette prices", var_completedBlock: nil).hide(animated: true, afterDelay: var_kHudHiddenTime)
                return
            }
            if var_cigarettePrice.count == 0 {
                HTClassHudView().ht_showToastOnView(var_view: self!.view, var_title: "Please set cigarette prices", var_completedBlock: nil).hide(animated: true, afterDelay: var_kHudHiddenTime)
                return
            }
            guard let var_mg = self?.var_mgLabel.text else {
                HTClassHudView().ht_showToastOnView(var_view: self!.view, var_title: "Please enter the tar content", var_completedBlock: nil).hide(animated: true, afterDelay: var_kHudHiddenTime)
                return
            }
            if var_mg.count == 0 {
                HTClassHudView().ht_showToastOnView(var_view: self!.view, var_title: "Please enter the tar content", var_completedBlock: nil).hide(animated: true, afterDelay: var_kHudHiddenTime)
                return
            }
            
            let var_model = SmokingCessationPlanModel(
                var_dailyCigaretteConsumption: var_dailyCigaretteConsumption,
                var_cigaretteBrand: var_cigaretteBrand,
                var_cigarettePrice: var_cigarettePrice,
                var_price: self?.var_price ?? "",
                var_cigarettes_per_pack: self?.var_total_number ?? "",
                var_tarContentPerCigarette: var_mg,
                var_date: HTClassTool.ht_obtainYearMonthDay(),
                var_cigarettes_smoked_today: self?.var_dataModel?.var_cigarettes_smoked_today ?? "")
            HTClassStorage.ht_SaveTodayPlan(var_model: var_model)
            self?.navigationController?.popViewController(animated: true)
        }
        self.view.addSubview(var_saveBtn)
        var_saveBtn.snp.makeConstraints { make in
            make.bottom.equalToSuperview().offset(-100)
            make.centerX.equalToSuperview()
            make.width.equalTo(198)
            make.height.equalTo(44)
        }
        var_saveBtn.layoutIfNeeded()
        var_saveBtn.ht_setVerticalGradient(startColor: UIColor("#5CEBFD"), endColor: UIColor("#0691F3"))
        
        if let var_model = HTClassStorage.ht_getToDayPlan() {
            self.var_dataModel = var_model
            var_DailyCigaretteTF.text = var_model.var_dailyCigaretteConsumption
            var_CigaretteBrandTF.text = var_model.var_cigaretteBrand
            var_CigarettePriceLabel.text = var_model.var_cigarettePrice
            var_mgLabel.text = var_model.var_tarContentPerCigarette
            var_price = var_model.var_price
            var_total_number = var_model.var_cigarettes_per_pack
        }
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.setNavigationBarHidden(false, animated: true)
    }
    
    
    func ht_DailyCigaretteConsumption() {
        let var_label = ht_getTitleLabel(var_title: "Daily cigarette consumption")
        var_bgView.addSubview(var_label)
        var_label.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(13)
            make.top.equalToSuperview().offset(28)
        }
        
        var_bgView.addSubview(var_DailyCigaretteTF)
        var_DailyCigaretteTF.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-13)
            make.centerY.equalTo(var_label.snp.centerY)
            make.height.equalTo(18)
            make.width.equalTo(100)
        }
        
        let var_lin = UILabel()
        var_lin.backgroundColor = UIColor("#464646")
        var_bgView.addSubview(var_lin)
        var_lin.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(13)
            make.right.equalToSuperview().offset(-13)
            make.top.equalTo(var_label.snp.bottom).offset(15)
            make.height.equalTo(1)
        }
    }
    
    func ht_CigaretteBrand() {
        let var_label = ht_getTitleLabel(var_title: "Cigarette brand")
        var_bgView.addSubview(var_label)
        var_label.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(13)
            make.top.equalToSuperview().offset(76)
        }
        
        var_bgView.addSubview(var_CigaretteBrandTF)
        var_CigaretteBrandTF.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-13)
            make.centerY.equalTo(var_label.snp.centerY)
            make.height.equalTo(18)
            make.width.equalTo(100)
        }
        
        let var_lin = UILabel()
        var_lin.backgroundColor = UIColor("#464646")
        var_bgView.addSubview(var_lin)
        var_lin.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(13)
            make.right.equalToSuperview().offset(-13)
            make.top.equalTo(var_label.snp.bottom).offset(15)
            make.height.equalTo(1)
        }
    }
    
    func ht_CigarettePriceLabel() {
        let var_label = ht_getTitleLabel(var_title: "Cigarette price")
        var_bgView.addSubview(var_label)
        var_label.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(13)
            make.top.equalToSuperview().offset(124)
        }
        
        var_bgView.addSubview(var_CigarettePriceLabel)
        var_CigarettePriceLabel.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-13)
            make.centerY.equalTo(var_label.snp.centerY)
            make.height.equalTo(18)
            make.width.equalTo(200)
        }
        
        let var_lin = UILabel()
        var_lin.backgroundColor = UIColor("#464646")
        var_bgView.addSubview(var_lin)
        var_lin.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(13)
            make.right.equalToSuperview().offset(-13)
            make.top.equalTo(var_label.snp.bottom).offset(15)
            make.height.equalTo(1)
        }
        
        let var_button = UIButton()
        var_button.onTap {
            HTClassQuitSmokingPopView.ht_showAnimated {[weak self] var_price, var_number in
                self?.var_price = var_price
                self?.var_total_number = var_number
                // 计算单根价格
                let var_one_price =  self?.ht_calculateUnitPrice(var_price: Double(var_price) ?? 0, var_quantity: Int(var_number) ?? 0) ?? 0
                self?.var_CigarettePriceLabel.text = "\(var_one_price)"
            }
        }
        var_button.backgroundColor = .clear
        var_bgView.addSubview(var_button)
        var_button.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(var_label.snp.top)
            make.bottom.equalTo(var_lin.snp.bottom)
        }
    }
    
    
    func ht_mgLabel() {
        let var_label = ht_getTitleLabel(var_title: "Tar content per cigarette (mg)")
        var_bgView.addSubview(var_label)
        var_label.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(13)
            make.top.equalToSuperview().offset(173)
        }
        
        var_bgView.addSubview(var_mgLabel)
        var_mgLabel.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-13)
            make.centerY.equalTo(var_label.snp.centerY)
            make.height.equalTo(18)
            make.width.equalTo(100)
        }
    }
    
    func ht_getTitleLabel(var_title:String) -> UILabel {
        let var_titleLabel = UILabel()
        var_titleLabel.textColor = .white
        var_titleLabel.text = var_title
        var_titleLabel.font = .systemFont(ofSize: 14, weight: .medium)
        return var_titleLabel
    }
    
    func ht_calculateUnitPrice(var_price: Double, var_quantity: Int) -> Double {
        // 计算单根的价格
        let var_unitPrice = var_price / Double(var_quantity)
        // 保留两位小数
        let var_roundedUnitPrice = (var_unitPrice * 100).rounded() / 100
        return var_roundedUnitPrice
    }
}
